import ali from '../../assets/images/ali.jpg';
import baidu from '../../assets/images/baidu.jpg';
import { getPanLoginCheck, getCode } from '../../api/index.js'
Page({
  data() {
    return {
      ali: ali,
      baidu: baidu,
      url: null,
    }
  },
  methods: {
    async getPanLoginCheck(val) {
      const { data } = await getPanLoginCheck({})
      if (data.code === 500 && data.msg === '用户登录已超时，请重新登录') {
        ok.clearH5AppStorage().then(res=>{
          this.$router.replace('/pages/login/index')
        })
        .catch(err=> {
          console.log("接口调用异常",err);
        });
        return
      }
      const { bdIsLogin, alIsLogin } = data?.data


      if (bdIsLogin && val === 'baidu') {
        this.$router.push({ path: '/pages/info/index', query: { panType: val === 'ali' ? 3 : 2 } })
        return
      }

      if (alIsLogin && val === 'ali') {
        this.$router.push({ path: '/pages/info/index', query: { panType: val === 'ali' ? 3 : 2 } })
        return
      }

      setTimeout(()=>{
        this.getPanLoginCheck(val)
      },2000)
    },
    async goTo(val) {

      try {

        const { data } = await getPanLoginCheck({})
        if (data.code === 500 && data.msg === '用户登录已超时，请重新登录') {
          ok.clearH5AppStorage().then(res=>{
            this.$router.replace('/pages/login/index')
          })
          .catch(err=> {
            console.log("接口调用异常",err);
          });
          return
        }
        console.log(data)
        const { bdIsLogin, alIsLogin } = data?.data
        console.log(bdIsLogin);

        if (bdIsLogin && val === 'baidu') {
          this.$router.push({ path: '/pages/info/index', query: { panType: val === 'ali' ? 3 : 2 } })
          return
        }

        if (alIsLogin && val === 'ali') {
          this.$router.push({ path: '/pages/info/index', query: { panType: val === 'ali' ? 3 : 2 } })
          return
        }

        this.$Modal.confirm({
          title: '授权提示',
          content: '授权成功后,需要手动点击左上角返回按键',
          submitText: '去授权',
          cancelText: '取消',
          onCancel: () => {
            console.log('cancel')
          },
          onSubmit: async () => {

            await this.getCode(val)

            // webview因为无法关闭,所以只能使用iframe
            // 打开webview,让用户自己授权一下. 
            // 目前文档中没有找到如何关闭这个webview的方法.
            // ok.launchWebView(url)
            //   .catch(err => {
            //     console.log("接口调用异常", err);
            //   });
          }
        })


      } catch (error) {
        console.log(error);
        ok.showToast('系统错误,请联系管理员')
      }

    },

    async getCode(val) {
      let panType = 2
      if (val === 'ali') panType = 3
      const codeData = await getCode({ panType })
      if (codeData.data.code === 500 && codeData.data.msg === '用户登录已超时，请重新登录') {
        ok.clearH5AppStorage().then(res=>{
          this.$router.replace('/pages/login/index')
        })
        .catch(err=> {
          console.log("接口调用异常",err);
        });
        return
      }
      const url = codeData.data.data
      ok.launchWebView(url)
      .catch(err => {
        console.log("接口调用异常", err);
      });
      // this.url = url

      // this.getPanLoginCheck(val)

    }
  },
  mounted() {

  }
});
